package com.itgaoshu.hospitalproject.mapper;

import com.itgaoshu.hospitalproject.bean.Menu;
import com.itgaoshu.hospitalproject.bean.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface MenuMapper {
    //查询所有菜单   模糊查询
    List<Menu> selectMenuAllList(Menu menu);

    //添加菜单
    @Insert("insert into sys_menu(pid,title,href,spread,target,icon,available)values(#{pid},#{title},#{href},#{spread},null,#{icon},#{available})")
    int addMenu(Menu menu);

    //删除菜单
    @Delete("delete from sys_menu where id=#{id}")
    int deleteMenu(int id);

    //修改菜单
    @Update("update sys_menu set pid=#{pid},title=#{title},href=#{href},spread=#{spread},target=#{target},icon=#{icon},available=#{available} where id=#{id}")
    int updateMenu(Menu menu);

    //检查菜单是否有子菜单
    @Select("select count(1) from sys_menu s1,sys_menu s2 where s1.id=s2.pid and s1.id=#{id}")
    int checkMenuHasChildren(int pid);

    //登录的方法
    User login(String loginname);

    //根据用户id来查询菜单
    @Select("select DISTINCT t.*  from sys_menu t,(select m.userid,m.rid,rm.mid from sys_role_menu rm,(select ru.rid,u.userid  from sys_user u ,sys_role_user ru where u.userid = ru.uid and u.userid = #{userid}) m where rm.rid = m.rid)n where t.id = n.mid")
    List<Menu> selectMenuById(int userid);

    //加载角色可用菜单
    @Select("select *from sys_menu where available=1")
    List<Menu> loadAvailableMenu();

    //根据用户的角色id查找菜单内容
    @Select("select distinct t1.* from sys_menu t1 inner join sys_role_menu t2 inner join sys_role_user t3 inner join sys_role t4 on(t1.id=t2.mid and t2.rid=t3.rid and t2.rid=t4.roleid) where t3.uid=#{uid} and t1.available=1 and t4.available=1")
    List<Menu> selectMenuByRoleId(int roleid);
}
